import { IResponse } from './type';

const MainRequest = <T>(data: UniApp.RequestOptions) => {
    return new Promise((resolve, reject) => {
        uni.request({
            ...data,
            success(result) {
                if (result.code == 200) {
                    let data = result.data as T;
                    resolve(data)
                }
            },
            fail(error) {
                reject(error)
            }
        })
    })
};

uni.addInterceptor('request', {
    invoke(args) {
        //可以设置公共参数 和 header
        console.log(args.header);
        console.log(args.data);

    },
    returnValue(args) {
        // 只返回 data 字段
        return args.data
    }
})



export async function userRequest<T>(data) {
    return (await MainRequest(data)) as unknown as IResponse<T>
}